package com.wpml.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.wpml.common.constant.DatePatternConstant;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @description:
 * @author: wp
 * @date: 2019-09-02 10:43
 **/
public class DateUtils {

    // 时区
    private static ZoneId zoneId = ZoneId.systemDefault();

    public static String date2String(Date date, String pattern) {

        if (date == null) {
            throw new IllegalArgumentException("时间参数 date 不能为空");
        }

        if (StrUtil.isBlank(pattern)) {
            pattern = DatePatternConstant.YYYY_MM_DD_HH_MM_SS;
        }

        return DateUtil.format(date, pattern);
    }

    public static Date str2Date(String dateStr, String pattern) {
        if (StrUtil.isBlank(dateStr)) {
            throw new IllegalArgumentException("时间参数 dateStr 不能为空");
        }

        if (StrUtil.isBlank(pattern)) {
            pattern = DatePatternConstant.YYYY_MM_DD_HH_MM_SS;
        }

        return DateUtil.parse(dateStr, pattern);
    }

    public static LocalDateTime str2LocalDateTime(String dateStr, String pattern) {

        if (StrUtil.isBlank(dateStr)) {
            throw new IllegalArgumentException("时间参数 dateStr 不能为空");
        }

        if (StrUtil.isBlank(pattern)) {
            pattern = DatePatternConstant.YYYY_MM_DD_HH_MM_SS;
        }
        return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
    }

    public static LocalDate str2LocalDate(String dateStr, String pattern) {

        if (StrUtil.isBlank(dateStr)) {
            throw new IllegalArgumentException("时间参数 dateStr 不能为空");
        }

        if (StrUtil.isBlank(pattern)) {
            pattern = DatePatternConstant.YYYY_MM_DD;
        }
        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
    }

    public static String localDate2Str(LocalDate localDate, String pattern) {

        if (localDate == null) {
            throw new IllegalArgumentException("时间参数 localDate 不能为空");
        }

        if (StrUtil.isBlank(pattern)) {
            pattern = DatePatternConstant.YYYY_MM_DD;
        }

        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String localDateTime2Str(LocalDateTime localDateTime, String pattern) {

        if (localDateTime == null) {
            throw new IllegalArgumentException("时间参数 localDateTime 不能为空");
        }

        if (StrUtil.isBlank(pattern)) {
            pattern = DatePatternConstant.YYYY_MM_DD_HH_MM_SS;
        }

        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * LocalDate转 date
     */
    public static Date localDate2Date(LocalDate localDate) {
        Instant instant = localDate.atStartOfDay().atZone(zoneId).toInstant();
        return Date.from(instant);
    }

    /**
     * LocalDateTime 转 date
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(zoneId).toInstant();
        return Date.from(instant);
    }

    /**
     * date 转 LocalDateTime
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    /**
     * date 转 LocalDateTime
     */
    public static LocalDate date2LocalDate(Date date) {
        Instant instant = date.toInstant();

        return LocalDateTime.ofInstant(instant, zoneId).toLocalDate();
    }


}
